package com.co.user.controller;

import com.co.common.entity.Result;
import com.co.user.pojo.Like;
import com.co.user.service.LikeService;
import com.co.video.pojo.Detail;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @author ldl.plus
 * @date 2020年06月22日  0:41
 */

@RestController
@RequestMapping("/user/like")

public class LikeController {

    @Autowired
    private LikeService likeService;

    /**
     * UsLike分页条件搜索实现
     *
     * @param like
     * @param page
     * @param size
     * @return
     */
    @PostMapping(value = "/search/{page}/{size}")
    public Result<PageInfo<Like>> findPage(@RequestBody(required = false) Like like, @PathVariable int page, @PathVariable int size) {
        //调用UsLikeService实现分页条件查询UsLike
        PageInfo<Like> pageInfo = likeService.findPage(like, page, size);
        return Result.ok(pageInfo);
    }

    /**
     * UsLike分页搜索实现
     *
     * @param page:当前页
     * @param size:每页显示多少条
     * @return
     */
    @GetMapping(value = "/search/{page}/{size}")
    public Result<PageInfo<Like>> findPage(@PathVariable int page, @PathVariable int size) {
        //调用UsLikeService实现分页查询UsLike
        PageInfo<Like> pageInfo = likeService.findPage(page, size);
        return Result.ok(pageInfo);
    }

    /**
     * 多条件搜索品牌数据
     *
     * @param like
     * @return
     */
    @PostMapping(value = "/search")
    public Result<List<Like>> findList(@RequestBody(required = false) Like like) {
        //调用UsLikeService实现条件查询UsLike
        List<Like> list = likeService.findList(like);
        return Result.ok(list);
    }

    /**
     * 根据ID删除品牌数据
     *
     * @param id
     * @return
     */
    @DeleteMapping(value = "/{id}")
    public Result<?> delete(@PathVariable String id) {
        //调用UsLikeService实现根据主键删除
        likeService.delete(id);
        return Result.ok();
    }

    /**
     * 清空个人喜爱
     *
     * @return
     */
    @DeleteMapping("/deleteAll")
    public Result deleteAll() {
        likeService.deleteAll();
        return Result.ok("个人喜爱全部删除");
    }

    /**
     * 修改UsLike数据
     *
     * @param like
     * @param id
     * @return
     */
    @PutMapping(value = "/{id}")
    public Result<?> update(@RequestBody Like like, @PathVariable String id) {
        //设置主键值
        like.setId(id);
        //调用UsLikeService实现修改UsLike
        likeService.update(like);
        return Result.ok();
    }

    /**
     * 新增UsLike数据
     *
     * @param like
     * @return
     */
    @PostMapping
    public Result<?> add(@RequestBody Like like) {
        //调用UsLikeService实现添加UsLike
        likeService.add(like);
        return Result.ok();
    }

    /**
     * 根据ID查询UsLike数据
     *
     * @param id
     * @return
     */
    @GetMapping("/{id}")
    public Result<Like> findById(@PathVariable String id) {
        //调用UsLikeService实现根据主键查询UsLike
        Like like = likeService.findById(id);
        return Result.ok(like);
    }

    /**
     * 查询UsLike全部数据
     *
     * @return
     */
    @GetMapping
    public Result<List<Like>> findAll() {
        //调用UsLikeService实现查询所有UsLike
        List<Like> list = likeService.findAll();
        return Result.ok(list);
    }

    /**
     * 根据用户名查询用户喜爱列表
     *
     * @param username
     * @return
     */
    @GetMapping("/user/{username}")
    public Result<Object> findHistoryByUsername(@PathVariable("username") String username) {
        List<Detail> historyByUsername = likeService.findLikeByUsername(username);
        return Result.ok(historyByUsername);
    }
}
